// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const xlsx=require('node-xlsx')
const db=cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  const fileID=event.fileID;
  const res=await cloud.downloadFile({
    fileID: fileID
  })
  const buffer=res.fileContent
  const tasks=[]
  const student_obj={}
  var sheets=xlsx.parse(buffer)
  sheets.forEach(function(sheet){
    console.log(sheet['name']);
    for (var rowId in sheet['data']) {
      console.log(rowId);
      var row = sheet['data'][rowId]; //第几行数据
      if (rowId > 0 && row) { //第一行是表格标题，所有我们要从第2行开始读
        //3，把解析到的数据存到excelList数据表里
        student_obj.stu_id=''+row[0];
        student_obj.stu_name=''+row[1];
        student_obj.stu_password=''+row[2];
        student_obj.stu_type=row[3];
        student_obj.stu_note=row[4]!=undefined?row[4]:'';
        student_obj.stu_phone='';
        student_obj.stu_mail='';
        student_obj.stu_birth='';
        student_obj.stu_location='';
        student_obj.stu_hope_job='';
        student_obj.stu_hope_location='';
        student_obj.stu_hope_pay='';
        student_obj.stu_self='';
        student_obj.openid='';
        student_obj.jobs_id=[];
        const promise = db.collection('student')
          .add({
            data: {
            
              student_obj: student_obj
            }
          })
        tasks.push(promise)
      }
    }
  });
  let result = await Promise.all(tasks).then(res => {
    return res
  }).catch(function(err) {
    return err
  })
  await cloud.deleteFile({fileList: [fileID]})
  return result
}